package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.qo.QueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class RechargeOfflineServiceImpl implements IRechargeOfflineService {
    @Autowired
    private RechargeOfflineMapper rechargeOfflineMapper;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountFlowService;

    @Override
    public int save(RechargeOffline rechargeOffline) {
        return rechargeOfflineMapper.insert(rechargeOffline);
    }

    @Override
    public int update(RechargeOffline rechargeOffline) {
        return rechargeOfflineMapper.updateByPrimaryKey(rechargeOffline);
    }

    @Override
    public RechargeOffline get(Long id) {
        return rechargeOfflineMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List result = rechargeOfflineMapper.queryData(qo);
        return new PageInfo(result);
    }

    @Override
    public void apply(RechargeOffline rechargeOffline) {
        RechargeOffline ro = new RechargeOffline();
        ro.setAmount(rechargeOffline.getAmount());
        ro.setBankInfo(rechargeOffline.getBankInfo());
        ro.setNote(rechargeOffline.getNote());
        ro.setRechargeDate(rechargeOffline.getRechargeDate());
        ro.setTradeCode(rechargeOffline.getTradeCode());
        ro.setApplier(UserContext.getCurrent());
        ro.setApplyTime(new Date());
        ro.setState(RechargeOffline.STATE_NORMAL);
        this.save(ro);
    }

    @Override
    public void audit(Long id, String remark, Integer state) {
        //获取审核对象
        RechargeOffline ro = this.get(id);
        if (ro != null && ro.getState() == RechargeOffline.STATE_NORMAL) {
            //设置相关的属性
            ro.setAuditor(UserContext.getCurrent());
            ro.setAuditTime(new Date());
            ro.setRemark(remark);
            ro.setState(state);
            if (state == RechargeOffline.STATE_PASS) {
                //如果审核通过,设置账户相关信息
                Account applierAccount = accountService.select(ro.getApplier().getId());
                applierAccount.setUsableAmount(applierAccount.getUsableAmount().add(ro.getAmount()));
                accountService.update(applierAccount);
                //记录流水信息
                accountFlowService.createRechargeOfflineFlow(applierAccount, ro.getAmount());
            }
            this.update(ro);
        }


    }
}
